﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Accounting;
using School.Common;

namespace School.Pages.Accounting
{
    public partial class Treasury : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1027) == true)
                {
                    if (!IsPostBack)
                    {
                        ListBox1.DataSource = Expense.GetAllExpenses();
                        ListBox1.DataTextField = "Name";
                        ListBox1.DataValueField = "ID";
                        ListBox1.DataBind();
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<long> expenses = new List<long>();

            foreach (ListItem i in ListBox1.Items)
            {
                if (i.Selected)
                    expenses.Add(long.Parse(i.Value));
            }

            DateTime fromDate = new DateTime(0001, 1, 1);
            DateTime toDate = new DateTime(9999, 12, 31);
            
            /*if (TxtFrom.Text != "")
            {
                fromDate = DateTime.Parse(TxtFrom.Text);                
            }
            if (TxtTo.Text != "")
            {
                toDate = DateTime.Parse(TxtTo.Text);
                toDate.AddHours(23);
                toDate.AddMinutes(59);
                toDate.AddSeconds(59);
            }*/

            fromDate = DateTime.Today;
            toDate = fromDate.AddHours(23).AddMinutes(59).AddSeconds(59);
                
            var v = Voucher.Search(fromDate, toDate, expenses).Where(x => x.Is_Active == true);
            GridView1.DataSource = v.Select(x => new { x.Number, x.Date, x.Value, SName = x.TE_Student.Ar_FirstName + " " + x.TE_Student.Ar_SecondName + " " + x.TE_Student.Ar_ThirdNmae + " " + x.TE_Student.Ar_FourthName, Expense_Name = x.ACC_Expense.Name, Student_Number = x.TE_Student.RegistrationNumber });

            GridView1.DataBind();

            if (GridView1.Rows.Count > 0)
            {
                Panel1.Visible = true;
                lblTotal.Text = v.Sum(x => x.Value).ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            /*foreach (GridViewRow i in GridView1.Rows)
            {
                CheckBox c = (CheckBox)i.FindControl("Chk");
                Label l = (Label)i.FindControl("LblNumber");

                if (c.Checked)
                {
                    Voucher.SetFalse(long.Parse(l.Text));
                }
            }
            Panel1.Visible = false;
            Response.Redirect("Treasury.aspx");*/
        }
    }
}